#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2001,2002 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)49       1.4  src/rsct/rm/ServiceRM/utils/addrmcacl, ServiceRM, rsct_rpyxh, rpyxht1f2 10/14/02 11:42:18

if ( ($#ARGV <0) || (($#ARGV+1)>=4 ) )
{
  print <<FNUSAGETEXT;
usage : addrmcacl <Group> <Machine Name> <permissions>

FNUSAGETEXT
exit 1;
}

$groupname=$ARGV[0];
$NodeName=$ARGV[1];
$Nodeperm=$ARGV[2];

$cnt=0;
$dupcnt=0;
$chkflg=0;
&checkduplicates;

if ($dupcnt >=1)
{
   ## print "Duplicate entry ...entry is already there\n";
   ;
}
else
{
   open(TEMP,">/tmp/temp_acls") || die print "Can't create temp_acls file :$!\n";
   $tempfilehandle=select(TEMP);

   $cmd="\/var\/ct\/cfg\/ctrmc\.acls";
   if (! -f $cmd)
   {
      $cmd="cp \/usr\/sbin\/rsct\/cfg\/ctrmc\.acls \/var\/ct\/cfg\/ctrmc\.acls";
      system($cmd);
   }
   open(ACLS,"/var/ct/cfg/ctrmc.acls") || die print "Can't open ctrmc.acls file :$!\n";

   while(<ACLS>)
   {
      print;
      if (/^$groupname/)
      {
         print "\t $NodeName \t\* $Nodeperm\n";
         $cnt=+1;
      }
   }
   close ACLS;
   select($tempfilehandle);
   close TEMP;
   if ($cnt==0)
   {
     open(TEMP,">>/tmp/temp_acls") || die print "Can't create temp_acls file :$!\n";

     print TEMP "$groupname \n";
     print TEMP "\t $NodeName \t\* $Nodeperm\n";
     close TEMP;
   }
   system("mv /tmp/temp_acls /var/ct/cfg/ctrmc.acls"); 
} 

sub checkduplicates
{
   $cmd="\/var\/ct\/cfg\/ctrmc\.acls";
   if (! -f $cmd)
   {
      $cmd="cp \/usr\/sbin\/rsct\/cfg\/ctrmc\.acls \/var\/ct\/cfg\/ctrmc\.acls";
      system($cmd);
   }
   open(ACLS,"/var/ct/cfg/ctrmc.acls") || die print "Can't open ctrmc.acls file :$!\n";
   while(<ACLS>)
   {
     if (/^IBM/)
     {
        if (/^$groupname/)
        {
        $chkflg=1;
        }
        else
        {
        $chkflg=0;
        }
     }
     else
     {
       if ($chkflg==0)
       {
          if (/^DEFAULT/)
          {
            if (/^$groupname/)
            {
            $chkflg=1;
            }
            else
            {
            $chkflg=0;
            }
          }
       }
     }

     if ($chkflg==1)
     {
       if (/$NodeName/)
       {
         $dupcnt=+1;
       }
     }
   }
close ACLS;
}
 
